﻿using EFCoreRepository.Extensions;
using Microsoft.Data.SqlClient;
using System;
using System.Windows.Forms;
using RescueApp.Data;
using RescueApp.Models;

namespace RescueApp.UI
{
    public partial class frmNewRescueApplication : Form
    {
        private RescueContext db;

        public frmNewRescueApplication()
        {
            InitializeComponent();
        }

        public frmNewRescueApplication(RescueContext db)
        {
            InitializeComponent();
            this.db = db;
        }

        private void btn_submit_Click(object sender, EventArgs e)
        {

            // 获取输入值
            int rescueID;
            rescueID = int.Parse(txt_rescueID.Text);
            string vehicleLicensePlate = txt_vehicleLicensePlate.Text.Trim();
            string carType = txt_carType.Text.Trim();
            string driverLicenseID = txt_driverLicenseID.Text.Trim();
            string phone = txt_phone.Text.Trim();
            string rescueLocation = txt_rescueLocation.Text.Trim();
            string destination = txt_destination.Text.Trim();

            // 验证所有字段是否已填写
            if (string.IsNullOrEmpty(vehicleLicensePlate) ||
                string.IsNullOrEmpty(carType) ||
                string.IsNullOrEmpty(driverLicenseID) ||
                string.IsNullOrEmpty(phone) ||
                string.IsNullOrEmpty(rescueLocation) ||
                string.IsNullOrEmpty(destination))
            {
                MessageBox.Show("请填写完整信息！");
                return;
            }

            string sql = @"
            -- 启用标识列显式插入
            SET IDENTITY_INSERT RescueApply ON;
            INSERT INTO [dbo].[RescueApply] (
                RescueID,
                VehicleLicensePlate,
                CarType,
                DriverIdentification,
                ApplicantPhone,
                RescueLocation,
                RepairDestination,
                ApplicationDate
            ) VALUES (
                @RescueID,
                @VehicleLicensePlate,
                @CarType,
                @DriverIdentification,
                @ApplicantPhone,
                @RescueLocation,
                @RepairDestination,
                @ApplicationDate
            );
            -- 禁用标识列显式插入
            SET IDENTITY_INSERT RescueApply OFF";

            IDictionary<string, object> dict = new Dictionary<string, object>();
            dict["@RescueID"] = rescueID;
            dict["@VehicleLicensePlate"] = vehicleLicensePlate;
            dict["@CarType"] = carType;
            dict["@DriverIdentification"] = driverLicenseID;
            dict["@ApplicantPhone"] = phone;
            dict["@RescueLocation"] = rescueLocation;
            dict["@RepairDestination"] = destination;
            dict["@ApplicationDate"] = DateTime.Now;

            SqlParameter[] parameters = dict.ToSqlParameters();
            int affected = db.ExecuteSql(sql, parameters);

            if (affected > 0)
            {
                MessageBox.Show("救援申请提交成功！");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("救援申请提交失败！");
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txt_vehicleLicensePlate_TextChanged(object sender, EventArgs e)
        {

        }

        private void txt_rescueID_TextChanged(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void txt_vehicleLicensePlate_TextChanged_1(object sender, EventArgs e)
        {

        }
    }
}